home *** CD-ROM | disk | FTP | other *** search
- package tetris;
-
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.util.Random;
- import java.util.Timer;
- import javax.microedition.lcdui.Canvas;
- import javax.microedition.lcdui.Command;
- import javax.microedition.lcdui.CommandListener;
- import javax.microedition.lcdui.Display;
- import javax.microedition.lcdui.Displayable;
- import javax.microedition.lcdui.Form;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.rms.RecordStore;
-
- public class GameScreen extends Canvas implements CommandListener, Runnable {
- static int cols = 11;
- static int rows;
- static int blockSize;
- // $FF: renamed from: oX int
- static int field_0 = 1;
- // $FF: renamed from: oY int
- static int field_1;
- // $FF: renamed from: nX int
- static int field_2;
- // $FF: renamed from: nY int
- static int field_3 = 1;
- // $FF: renamed from: sX int
- static int field_4;
- // $FF: renamed from: sY int
- static int field_5;
- static boolean colored;
- static int[][][] blocks = new int[][][]{{{1, 0}, {1, 1}, {1, 2}, {1, 3}}, {{0, 1}, {1, 1}, {2, 1}, {3, 1}}, {{1, 0}, {1, 1}, {1, 2}, {2, 2}}, {{0, 1}, {1, 1}, {2, 1}, {2, 0}}, {{1, 0}, {2, 0}, {2, 1}, {2, 2}}, {{0, 0}, {1, 0}, {2, 0}, {0, 1}}, {{2, 0}, {2, 1}, {2, 2}, {1, 2}}, {{0, 1}, {1, 1}, {2, 1}, {2, 2}}, {{1, 0}, {1, 1}, {1, 2}, {2, 0}}, {{0, 0}, {0, 1}, {1, 1}, {2, 1}}, {{0, 0}, {1, 0}, {1, 1}, {2, 1}}, {{1, 0}, {1, 1}, {0, 1}, {0, 2}}, {{0, 1}, {1, 1}, {1, 0}, {2, 0}}, {{0, 0}, {0, 1}, {1, 1}, {1, 2}}, {{1, 0}, {1, 1}, {2, 1}, {1, 2}}, {{0, 1}, {1, 0}, {1, 1}, {2, 1}}, {{0, 1}, {1, 0}, {1, 1}, {1, 2}}, {{0, 0}, {1, 0}, {2, 0}, {1, 1}}, {{0, 0}, {1, 0}, {1, 1}, {0, 1}}};
- static int[] blockClass = new int[]{2, 4, 4, 2, 2, 4, 1};
- static int[] blockClassCount = new int[]{0, 2, 6, 10, 12, 14, 18};
- static int[] colors = new int[]{16711680, 16711680, 16711935, 16711935, 16711935, 16711935, 65535, 65535, 65535, 65535, 65280, 65280, 255, 255, 16744384, 16744384, 16744384, 16744384, 16744192};
- static int[] rotation = new int[]{1, 0, 3, 4, 5, 2, 7, 8, 9, 6, 11, 10, 13, 12, 15, 16, 17, 14, 18};
- static int[][] nextIndent = new int[][]{{1, 0}, {0, 1}, {0, 1}, {1, 2}, {0, 1}, {1, 2}, {0, 1}, {1, 0}, {0, 1}, {1, 2}, {1, 2}, {2, 1}, {1, 2}, {2, 1}, {0, 1}, {1, 2}, {2, 1}, {1, 2}, {2, 2}};
- static int scoreFontSize = 3;
- static int nrRowsOutToChangeLevel = 25;
- static int defaultGameSpeed = 500;
- static Random gameRND = new Random();
- static int startItemX;
- static int startItemY = 0;
- static int keyLeft;
- static int keyRight;
- static int keyUp;
- static int keyDown;
- static Command pauseCommand = new Command("Pause", 1, 1);
- static Command unpauseCommand = new Command("Unpause", 4, 1);
- static Command backCommand = new Command("Menu", 7, 2);
- static RecordStore gameRS;
- Timer gameTimer;
- int gameSpeed = 500;
- int gameLevel = 1;
- boolean gameOver;
- boolean paused;
- int currentItem;
- int currentItemX;
- int currentItemY;
- int oldItem;
- int oldItemX;
- int oldItemY;
- int nextItem;
- boolean clearOldItem;
- boolean fullRepaint;
- boolean moved;
- boolean paintNext;
- boolean itemStopped;
- byte[][] playField;
- int gameScore;
- int gameLines;
-
- public GameScreen() {
- try {
- this.jbInit();
- } catch (Exception e) {
- ((Throwable)e).printStackTrace();
- }
-
- }
-
- private void jbInit() throws Exception {
- ((Displayable)this).setCommandListener(this);
- if (((Canvas)this).getWidth() >= 96 && ((Canvas)this).getHeight() >= 54) {
- blockSize = 4;
- field_1 = (((Canvas)this).getHeight() - blockSize * cols) / 1 - 1;
- } else {
- blockSize = 2;
- field_1 = 1;
- }
-
- rows = (((Canvas)this).getWidth() - 8 - 4) / blockSize;
- startItemX = (cols - 4) / 2 + 1;
- field_2 = ((Canvas)this).getWidth() - 8 - 1;
- field_4 = ((Canvas)this).getWidth() - 2 * scoreFontSize - 2;
- field_5 = field_3 + 8 + 2 + scoreFontSize;
- if (!Options.readKeys()) {
- Options.keyLeft = ((Canvas)this).getKeyCode(1);
- Options.keyRight = ((Canvas)this).getKeyCode(6);
- Options.keyUp = 53;
- Options.keyDown = ((Canvas)this).getKeyCode(2);
- }
-
- this.playField = new byte[rows][cols];
- }
-
- protected int getGameSpeed(int level) {
- int speed = defaultGameSpeed;
-
- for(int i = 1; i < level; ++i) {
- speed -= speed > 100 ? 100 : 10;
- }
-
- return speed;
- }
-
- protected void initGame() {
- if (Options.optDefaultKeys) {
- keyLeft = ((Canvas)this).getKeyCode(1);
- keyRight = ((Canvas)this).getKeyCode(6);
- keyUp = 53;
- keyDown = ((Canvas)this).getKeyCode(2);
- } else {
- keyLeft = Options.keyLeft;
- keyRight = Options.keyRight;
- keyUp = Options.keyUp;
- keyDown = Options.keyDown;
- }
-
- this.fullRepaint = true;
- colored = Display.getDisplay(TetrisMIDlet.instance).numColors() > 2;
- }
-
- private void createGameTimer() {
- this.gameTimer = new Timer();
- this.gameTimer.scheduleAtFixedRate(new GameTimerTask(this), 0L, (long)this.gameSpeed);
- }
-
- private void startGameTimer() {
- ((Displayable)this).removeCommand(unpauseCommand);
- ((Displayable)this).removeCommand(backCommand);
- ((Displayable)this).addCommand(pauseCommand);
- ((Displayable)this).addCommand(backCommand);
- this.gameOver = false;
- this.createGameTimer();
- this.paused = false;
- }
-
- public void startGame() {
- this.initGame();
-
- for(int i = 0; i < rows; ++i) {
- for(int j = 0; j < cols; ++j) {
- this.playField[i][j] = 0;
- }
- }
-
- this.generateNextItem();
- this.newItem();
- this.gameScore = 0;
- this.gameLines = 0;
- this.gameLevel = Options.startLevel + 1;
- this.gameSpeed = this.getGameSpeed(this.gameLevel);
-
- for(int i = 1; i <= Options.garbageLevel * 2; ++i) {
- boolean full = true;
-
- while(full) {
- for(int j = 0; j < cols; ++j) {
- int b;
- for(b = -1; b < 0; b = gameRND.nextInt() % (blocks.length + blocks.length / 3)) {
- }
-
- this.playField[rows - i][j] = (byte)(b < blocks.length ? b + 1 : 0);
- if (b == 0) {
- full = false;
- }
- }
- }
- }
-
- this.startGameTimer();
- }
-
- public void resumeGame() {
- this.initGame();
- if (Options.optProgressive) {
- for(int x = this.gameLines - (this.gameLevel - 1) * nrRowsOutToChangeLevel; x >= nrRowsOutToChangeLevel; x -= nrRowsOutToChangeLevel) {
- ++this.gameLevel;
- }
-
- this.gameSpeed = this.getGameSpeed(this.gameLevel);
- }
-
- this.startGameTimer();
- }
-
- public void stopGame() {
- if (!this.gameOver) {
- this.saveGame();
- }
-
- this.gameTimer.cancel();
- this.paused = true;
- }
-
- protected static void openStore() {
- try {
- gameRS = RecordStore.openRecordStore("savegame", true);
- } catch (Exception e) {
- ((Throwable)e).printStackTrace();
- }
-
- }
-
- protected static void closeStore() {
- try {
- gameRS.closeRecordStore();
- } catch (Exception e) {
- ((Throwable)e).printStackTrace();
- }
-
- }
-
- static boolean existsSavedGame() {
- openStore();
- boolean b = false;
-
- try {
- int x = gameRS.getNumRecords();
- b = x > 0;
- } catch (Exception e) {
- ((Throwable)e).printStackTrace();
- }
-
- closeStore();
- return b;
- }
-
- public boolean loadGame() {
- openStore();
- boolean success = true;
-
- try {
- if (gameRS.getNumRecords() > 0) {
- byte[] b = gameRS.getRecord(1);
- ByteArrayInputStream bais = new ByteArrayInputStream(b);
- DataInputStream dis = new DataInputStream(bais);
-
- for(int i = 0; i < rows; ++i) {
- dis.read(this.playField[i], 0, this.playField[i].length);
- }
-
- this.nextItem = dis.readInt();
- this.currentItem = dis.readInt();
- this.currentItemX = dis.readInt();
- this.currentItemY = dis.readInt();
- this.clearOldItem = dis.readInt() == 1;
- this.paintNext = dis.readInt() == 1;
- this.moved = dis.readInt() == 1;
- this.gameScore = dis.readInt();
- this.gameLines = dis.readInt();
- this.gameLevel = dis.readInt();
- this.gameSpeed = this.getGameSpeed(this.gameLevel);
- this.fullRepaint = true;
- }
- } catch (Exception e) {
- success = false;
- ((Throwable)e).printStackTrace();
- }
-
- try {
- gameRS.deleteRecord(1);
- } catch (Exception e) {
- ((Throwable)e).printStackTrace();
- }
-
- closeStore();
-
- try {
- RecordStore.deleteRecordStore("savegame");
- } catch (Exception e) {
- ((Throwable)e).printStackTrace();
- }
-
- return success;
- }
-
- public void saveGame() {
- openStore();
-
- try {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- DataOutputStream dos = new DataOutputStream(baos);
-
- for(int i = 0; i < rows; ++i) {
- dos.write(this.playField[i], 0, this.playField[i].length);
- }
-
- dos.writeInt(this.nextItem);
- dos.writeInt(this.currentItem);
- dos.writeInt(this.currentItemX);
- dos.writeInt(this.currentItemY);
- dos.writeInt(this.clearOldItem ? 1 : 0);
- dos.writeInt(this.paintNext ? 1 : 0);
- dos.writeInt(this.moved ? 1 : 0);
- dos.writeInt(this.gameScore);
- dos.writeInt(this.gameLines);
- dos.writeInt(this.gameLevel);
- byte[] b = baos.toByteArray();
- if (gameRS.getNumRecords() > 0) {
- gameRS.setRecord(1, b, 0, b.length);
- } else {
- gameRS.addRecord(b, 0, b.length);
- }
- } catch (Exception e) {
- ((Throwable)e).printStackTrace();
- }
-
- closeStore();
- }
-
- public void run() {
- this.downItem();
- }
-
- public void commandAction(Command command, Displayable displayable) {
- if (command.getCommandType() == 7) {
- this.stopGame();
- if (!this.gameOver) {
- TetrisMIDlet.mainMenu.addContinue();
- }
-
- Display.getDisplay(TetrisMIDlet.instance).setCurrent(TetrisMIDlet.mainMenu);
- }
-
- if (command.getCommandType() == 1) {
- this.stopGame();
- ((Displayable)this).removeCommand(pauseCommand);
- ((Displayable)this).removeCommand(backCommand);
- ((Displayable)this).addCommand(unpauseCommand);
- ((Displayable)this).addCommand(backCommand);
- }
-
- if (command.getCommandType() == 4) {
- this.resumeGame();
- }
-
- if (command.getCommandType() == 2) {
- if (TetrisMIDlet.highScores.checkScore(this.gameScore)) {
- TetrisMIDlet.highScores.getName();
- } else {
- Display.getDisplay(TetrisMIDlet.instance).setCurrent(TetrisMIDlet.mainMenu);
- }
- }
-
- }
-
- protected void keyPressed(int keyCode) {
- if (!this.paused) {
- if (keyCode == keyLeft) {
- this.moveItem(-1);
- } else if (keyCode == keyRight) {
- this.moveItem(1);
- } else if (keyCode == keyUp) {
- this.rotateItem();
- } else if (keyCode == keyDown) {
- this.dropItem();
- }
- }
-
- }
-
- public void generateNextItem() {
- int nextClass;
- for(nextClass = -1; nextClass < 0; nextClass = gameRND.nextInt() % blockClass.length) {
- }
-
- int nextMember;
- for(nextMember = -1; nextMember < 0; nextMember = gameRND.nextInt() % blockClass[nextClass]) {
- }
-
- this.nextItem = blockClassCount[nextClass] + nextMember;
- }
-
- public void newItem() {
- this.currentItem = this.nextItem;
- this.generateNextItem();
- this.currentItemX = startItemX;
- this.currentItemY = startItemY;
- this.clearOldItem = false;
- this.paintNext = true;
- this.moved = false;
- this.fullRepaint = true;
- }
-
- public boolean checkItemPos(int item, int x, int y) {
- for(int i = 0; i < 4; ++i) {
- if (x + blocks[item][i][0] < 0 || x + blocks[item][i][0] >= cols || y + blocks[item][i][1] >= rows) {
- return false;
- }
-
- if (this.playField[y + blocks[item][i][1]][x + blocks[item][i][0]] != 0) {
- return false;
- }
- }
-
- return true;
- }
-
- public void downItem() {
- int rowsOut = 0;
- if (this.checkItemPos(this.currentItem, this.currentItemX, this.currentItemY + 1)) {
- ++this.currentItemY;
- this.moved = true;
- this.clearOldItem = true;
- ((Canvas)this).repaint();
- } else {
- this.itemStopped = true;
-
- for(int i = 0; i < 4; ++i) {
- this.playField[this.currentItemY + blocks[this.currentItem][i][1]][this.currentItemX + blocks[this.currentItem][i][0]] = (byte)(this.currentItem + 1);
- }
-
- boolean check = true;
-
- while(check) {
- check = false;
-
- for(int i = rows - 1; i >= 0; --i) {
- boolean full = true;
-
- for(int j = 0; j < cols; ++j) {
- if (this.playField[i][j] == 0) {
- full = false;
- break;
- }
- }
-
- if (full) {
- this.moved = true;
- ++rowsOut;
-
- for(int k = i; k > 0; --k) {
- for(int l = 0; l < cols; ++l) {
- this.playField[k][l] = this.playField[k - 1][l];
- }
- }
-
- for(int l = 0; l < cols; ++l) {
- this.playField[0][l] = 0;
- }
-
- this.fullRepaint = true;
- check = true;
- break;
- }
- }
- }
-
- if (rowsOut > 0) {
- boolean empty = true;
-
- for(int i = 0; i < cols; ++i) {
- if (this.playField[rows - 1][i] != 0) {
- empty = false;
- break;
- }
- }
-
- if (empty) {
- this.gameScore += 25;
- }
-
- this.gameScore += rowsOut * (this.gameLevel - 1);
- if (Options.optProgressive) {
- this.gameScore += rowsOut * 2 - 1;
- } else {
- this.gameScore += rowsOut;
- }
-
- this.gameLines += rowsOut;
- if (Options.optProgressive) {
- for(int x = this.gameLines - (this.gameLevel - 1) * nrRowsOutToChangeLevel; x >= nrRowsOutToChangeLevel; x -= nrRowsOutToChangeLevel) {
- ++this.gameLevel;
- }
-
- int newSpeed = this.getGameSpeed(this.gameLevel);
- if (this.gameSpeed != newSpeed) {
- this.gameTimer.cancel();
- this.gameSpeed = newSpeed;
- this.createGameTimer();
- }
- }
- }
-
- if (this.moved) {
- this.newItem();
- ((Canvas)this).repaint();
- } else {
- this.gameOver = true;
- this.stopGame();
- Form go = new Form("");
- go.append("GAME OVER\n ");
- go.append("\nScore:".concat(String.valueOf(String.valueOf(Integer.toString(this.gameScore)))));
- go.append("\nLines:".concat(String.valueOf(String.valueOf(Integer.toString(this.gameLines)))));
- ((Displayable)go).setCommandListener(this);
- ((Displayable)go).addCommand(new Command("OK", 2, 1));
- Display.getDisplay(TetrisMIDlet.instance).setCurrent(go);
- }
- }
-
- }
-
- public void moveItem(int changeX) {
- if (!this.itemStopped && this.checkItemPos(this.currentItem, this.currentItemX + changeX, this.currentItemY)) {
- this.currentItemX += changeX;
- this.clearOldItem = true;
- ((Canvas)this).repaint();
- }
-
- }
-
- public void rotateItem() {
- if (!this.itemStopped && this.checkItemPos(rotation[this.currentItem], this.currentItemX, this.currentItemY)) {
- this.currentItem = rotation[this.currentItem];
- this.clearOldItem = true;
- ((Canvas)this).repaint();
- }
-
- }
-
- public void dropItem() {
- while(!this.itemStopped) {
- this.downItem();
- }
-
- }
-
- protected void paintNextBlock(Graphics g, int item, int x, int y, int color) {
- if (colored) {
- g.setColor(color);
- } else {
- g.setColor(color == 16777215 ? color : 0);
- }
-
- g.fillRect(field_2 + (4 - y - 1) * 2 - nextIndent[item][1], field_3 + x * 2 + nextIndent[item][0], 2, 2);
- }
-
- protected void paintBlock(Graphics g, int x, int y, int color) {
- if (colored) {
- g.setColor(color);
- } else {
- g.setColor(color == 16777215 ? color : 0);
- }
-
- g.fillRect(field_0 + (rows - y - 1) * blockSize, field_1 + x * blockSize, blockSize, blockSize);
- }
-
- protected void paintItem(Graphics g, int item, int x, int y, int color) {
- for(int i = 0; i < 4; ++i) {
- this.paintBlock(g, x + blocks[item][i][0], y + blocks[item][i][1], color);
- }
-
- }
-
- protected void drawItem(Graphics g, int item, int x, int y) {
- this.paintItem(g, item, x, y, colors[item]);
- }
-
- protected void clearItem(Graphics g, int item, int x, int y) {
- this.paintItem(g, item, x, y, 16777215);
- }
-
- protected void paint(Graphics g) {
- if (this.fullRepaint) {
- g.setColor(16777215);
- g.fillRect(0, 0, ((Canvas)this).getWidth(), ((Canvas)this).getHeight());
- g.setColor(0);
-
- for(int i = 0; i < rows; ++i) {
- for(int j = 0; j < cols; ++j) {
- if (this.playField[i][j] != 0) {
- this.paintBlock(g, j, i, colors[this.playField[i][j] - 1]);
- }
- }
- }
-
- g.setColor(0);
- g.drawRect(field_0 - 1, field_1 - 1, rows * blockSize + 1, cols * blockSize + 1);
- Digit.drawVNumber(g, field_4, field_5, (long)this.gameScore, scoreFontSize);
- if (Options.optProgressive) {
- g.setColor(0);
- g.drawLine(1, 1, 1, 5);
- g.drawLine(2, 5, 3, 5);
- g.drawLine(5, 1, 7, 1);
- g.drawLine(5, 2, 5, 5);
- g.drawLine(6, 3, 6, 3);
- g.drawLine(6, 5, 7, 5);
- g.drawLine(11, 5, 9, 1);
- g.drawLine(11, 5, 13, 1);
- g.drawLine(15, 1, 17, 1);
- g.drawLine(15, 2, 15, 5);
- g.drawLine(16, 3, 16, 3);
- g.drawLine(16, 5, 17, 5);
- g.drawLine(19, 1, 19, 5);
- g.drawLine(20, 5, 21, 5);
- Digit.drawHNumber(g, 25, 1, (long)this.gameLevel, 2);
- }
- }
-
- if (Options.optPreview && (this.fullRepaint || this.paintNext)) {
- g.setColor(16777215);
- g.fillRect(field_2, field_3, 8, 8);
- g.setColor(0);
- g.drawRect(field_2 - 1, field_3 - 1, 9, 9);
-
- for(int i = 0; i < 4; ++i) {
- this.paintNextBlock(g, this.nextItem, blocks[this.nextItem][i][0], blocks[this.nextItem][i][1], colors[this.nextItem]);
- }
-
- this.paintNext = false;
- }
-
- this.fullRepaint = false;
- if (this.clearOldItem) {
- this.clearItem(g, this.oldItem, this.oldItemX, this.oldItemY);
- this.clearOldItem = false;
- }
-
- this.drawItem(g, this.currentItem, this.currentItemX, this.currentItemY);
- this.oldItem = this.currentItem;
- this.oldItemX = this.currentItemX;
- this.oldItemY = this.currentItemY;
- this.itemStopped = false;
- }
- }
-